package com.itheima.dao.impl;

import com.itheima.controller.ISubwayController;
import com.itheima.dao.ISubwayDao;
import com.itheima.dao.ISubwayDao;
import com.itheima.domain.Subway;

import java.io.*;
import java.util.ArrayList;

public class SubwayDaoImpl implements ISubwayDao {
    private static ArrayList<Subway> list = new ArrayList<>();

    static {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("kaoshi\\subway.txt"));
            String s;
            while ((s = reader.readLine()) != null) {
                String[] subwayInfo = s.split(",");
                Subway subway = new Subway(subwayInfo[0], subwayInfo[1], subwayInfo[2]);
                list.add(subway);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public ArrayList<Subway> findAll() {

        return list;
    }

    @Override
    public void add(Subway subway) throws IOException {
        list.add(subway);
        BufferedWriter writer = new BufferedWriter(new FileWriter("kaoshi\\subway.txt",true));
        writer.newLine();
        writer.write(subway.getSid()+","+subway.getName()+","+subway.getLuxian());
        writer.close();


    }


}
